package cn.edu.neusoft.cqy917.zyfypt_cqy917.model;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.RegisterListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.Registeriface;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.service.LoginService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;



public class RegisterModel implements Registeriface{
    private LoginService loginService;
    private Retrofit retrofit;
    private static final  String BASE_URL=
            "http://amicool.neusoft.edu.cn/";
    public RegisterModel(){
        retrofit =new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(
                        ScalarsConverterFactory.create())
                .build();

    }

    @Override
    public void getRegisterResult(String username, String passwd, String tel, final RegisterListener registerListener) {
        loginService =retrofit.create(LoginService.class);
        Call<String>call =loginService.register(username,passwd,tel);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    registerListener.onResponse(response.body());
                else
                    registerListener.onFail("error");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                registerListener.onFail("error");
            }
        });
    }
}
